package com.LachesismStorm.lachesism.member.controller;

import com.LachesismStorm.lachesism.core.common.response.ResponseResult;
import com.LachesismStorm.lachesism.core.util.AsyncUtil;
import com.LachesismStorm.lachesism.member.mapper.UmsUserMapper;
import com.LachesismStorm.lachesism.member.service.UmsMemberService;
import com.LachesismStorm.lachesism.member.vo.UmsMember;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@Api(tags = "用户登录注册管理")
@RequestMapping("/member")
public class UmsMemberController {

    @Resource
    private UmsMemberService memberService;
    @Resource
    private UmsUserMapper mapper;


    @ApiOperation(value = "用户手机号注册")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "UmsMember", value = "用户注册信息")
    })
    @PostMapping("/register")
    public ResponseResult register(@RequestBody UmsMember member) {
        String token = memberService.register(member);
        return ResponseResult.success(token, "登录成功");
    }

    @ApiOperation(value = "用户手机号登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "UmsMember", value = "用户登陆信息")
    })
    @PostMapping("/login")
    public ResponseResult login(@RequestBody UmsMember member) {
        String token = memberService.login(member);
        return ResponseResult.success(token, "登录成功");
    }

    @ApiOperation(value = "修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "UmsMember", value = "用户登陆信息")
    })
    @PostMapping("/updateInfo")
    public ResponseResult updateInfo(@RequestBody UmsMember member) {
        memberService.updateInfo(member);
        return ResponseResult.success(null, "修改成功");
    }

    @ApiOperation(value = "获取用户信息")
    @PostMapping("/getUserInfo")
    public ResponseResult getUserInfo() {
        UmsMember umsMember = memberService.getUserInfo();
        return ResponseResult.success(umsMember, "查找成功");
    }

    @PostMapping("/batchAdd")
    public ResponseResult addUser(@RequestParam Integer index) {
        AsyncUtil.executeAsync(() -> {
            List<UmsMember> umsMembers = new ArrayList<>();
            Date date = new Date();
            for (int j = 3000; j < 6000; j++) {
                UmsMember umsMember = new UmsMember();
                umsMember.setId(j);
                umsMember.setPhone("1999901"+j);
                umsMember.setPassword("$2a$10$DXBfNmS2ZbF0hw3sLELb0eRskbHZ/8VSLMQ583eFCdflmxMaYYFEW");
                umsMember.setNickName("用户01"+j);
                umsMember.setRealName("01"+j);
                umsMember.setUsername(umsMember.getPhone());
                umsMember.setGender("F");
                umsMember.setIcon("http://139.224.51.76:41000/image/1.png");
                umsMember.setIdCard("9999999"+umsMember.getPhone());
                umsMember.setNote("3000");
                umsMember.setHabits("看老头洗澡");
                umsMember.setBirthday(date);
                umsMember.setRemake("安徽合肥包河");
                umsMember.setRole(2);
                umsMembers.add(umsMember);
            }
            mapper.batchAddUser(umsMembers);
        });

        UmsMember umsMember = new UmsMember();
        return ResponseResult.success(umsMember, "查找成功");
    }

}
